home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_2 / fifolib / test.c < prev    next >
C/C++ Source or Header  |  1991-11-08  |  3KB  |  158 lines

  1.  
  2. /*
  3.  *  TEST.C
  4.  *
  5.  *  TEST [R/W][N] fifo_name
  6.  *
  7.  *  test fifo operation
  8.  *
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <libraries/dos.h>
  14. #include <clib/alib_protos.h>
  15. #include <clib/exec_protos.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <fcntl.h>
  20. #include "fifo.h"
  21.  
  22. typedef struct MsgPort    MsgPort;
  23. typedef struct Message    Message;
  24.  
  25. void *FifoBase;
  26. void *Fh;
  27. MsgPort *WaPort;
  28. char IBuf[256];
  29.  
  30. void DoWait(long);
  31. void myexit(void);
  32.  
  33.  
  34. main(ac, av)
  35. char *av[];
  36. {
  37.     if (ac != 3) {
  38.     fputs("TEST [R/W][N] fifo_name\n", stderr);
  39.     exit(1);
  40.     }
  41.     atexit(myexit);
  42.  
  43.     WaPort = CreatePort(NULL, 0);
  44.  
  45.     if (FifoBase = OpenLibrary(FIFONAME, 0)) {
  46.     long flags = FIFOF_NORMAL;
  47.  
  48.     fputs("fifo.library opened!\n", stderr);
  49.  
  50.     if (av[1][1] == 'N')
  51.         flags |= FIFOF_NBIO;
  52.  
  53.     switch(av[1][0]) {
  54.     case 'R':
  55.         if (Fh = OpenFifo(av[2], 4096, FIFOF_READ | flags)) {
  56.         long i = 0;
  57.         char *ptr;
  58.  
  59.         fprintf(stderr, "fifo is %d bytes\n", BufSizeFifo(Fh));
  60.         for (;;) {
  61.             long n = ReadFifo(Fh, &ptr, i);
  62.             if (n > 64)     /*  limit amount of data read/loop */
  63.             n = 64;
  64.             i = n;
  65.  
  66.             if (n < 0) {
  67.             fputs("EOF\n", stderr);
  68.             break;
  69.             }
  70.             if (n > 0) {
  71.             /*
  72.              *  just so other windows doesn't freeze while we
  73.              *  output kilobytes.
  74.              */
  75.  
  76.             write(1, ptr, n);
  77.             } else {        /*    n == 0    */
  78.             if (flags & FIFOF_NBIO) {
  79.                 DoWait(FREQ_RPEND);
  80.             } else {
  81.                 puts("0 bytes avail?");
  82.             }
  83.             }
  84.             chkabort();
  85.         }
  86.         }
  87.         break;
  88.     case 'W':
  89.         if (Fh = OpenFifo(av[2], 4096, FIFOF_WRITE | FIFOF_KEEPIFD | flags)) {
  90.         long n;
  91.  
  92.         fprintf(stderr, "fifo is %d bytes\n", BufSizeFifo(Fh));
  93.  
  94.         while ((n = read(0, IBuf, sizeof(IBuf))) > 0) {
  95.             long r;
  96.             long x = 5;
  97.  
  98. loop:
  99.             r = WriteFifo(Fh, IBuf, n);
  100.             if (r != n) {
  101.             if (r >= 0 && (flags & FIFOF_NBIO)) {
  102.                 DoWait(FREQ_WAVAIL);
  103.                 goto loop;
  104.             } else {
  105.                 fprintf(stderr, "write failed! %d\n", r);
  106.                 break;
  107.             }
  108.             }
  109.         }
  110.         }
  111.         break;
  112.     default:
  113.         fputs("bad command line", stderr);
  114.         break;
  115.     }
  116.     }
  117.     return(0);
  118. }
  119.  
  120. void
  121. myexit(void)
  122. {
  123.     if (Fh) {
  124.     CloseFifo(Fh, FIFOF_EOF);
  125.     Fh = 0;
  126.     }
  127.     if (FifoBase) {
  128.     CloseLibrary(FifoBase);
  129.     FifoBase = 0;
  130.     }
  131.     if (WaPort) {
  132.     DeletePort(WaPort);
  133.     WaPort = NULL;
  134.     }
  135. }
  136.  
  137. void
  138. DoWait(req)
  139. long req;
  140. {
  141.     Message msg;
  142.     long mask = 0;
  143.  
  144.     fputs("WAIT\n", stderr);
  145.     msg.mn_ReplyPort = WaPort;
  146.     RequestFifo(Fh, &msg, req);
  147.  
  148.     while (msg.mn_Node.ln_Type == NT_MESSAGE) {
  149.     mask = Wait(SIGBREAKF_CTRL_C | (1 << WaPort->mp_SigBit));
  150.     if (mask & SIGBREAKF_CTRL_C)
  151.         RequestFifo(Fh, &msg, FREQ_ABORT);
  152.     }
  153.     GetMsg(WaPort);
  154.     if (mask & SIGBREAKF_CTRL_C)
  155.     fputs("WAIT: ABORT\n", stderr);
  156. }
  157.  
  158.